// /*
//     在这里定义和用户相关的路由管理处理函数，供、router/user.js 模块进行调用
// */
// //导入数据库模块
const db = require('../db/index')
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const config = require('../config')
const sd = require('silly-datetime')



// //注册用户函数
exports.regUser = (req,res) => {
    //获取客户端提交的服务器信息
    const userinfo = req.body
    // 对表单中的数据，进行合法性的校验
    if(!userinfo.username || !userinfo.password){
        return res.send({ status:1 ,message : '账号或密码不能为空' })
    }

    
    //查询sql语句 
    const sqlStr = 'select * from login_data where username=?'
    db.query(sqlStr,userinfo.username, (err,results) => {
        // 执行失败
        if(err){
            // return res.send({ status:1 ,message: err.message })
            return res.cc(err)
        }

        if(results.length > 0){
            // return res.send({ status:1 ,message: '用户名已占用'})
            return res.cc('账号已占用')
        }
        
    })
    
    //调用bcrypt.hashSync()对密码加密
    userinfo.password = bcrypt.hashSync(userinfo.password,10)
    
    //定义插入新用户的 sql 语句
    const sql = 'insert into login_data set ?'
    //调用插入语句
    db.query(sql,{ username: userinfo.username ,password: userinfo.password }, (err,results) =>{
        
        // if(err) return res.send( {status:1,message:err.message})
        if(err) return res.cc(err)
        //判断影响行数
        if(results.affectedRows !== 1)return res.cc('注册失败')

        //注册用户成功
        // res.send({status:0,message:'注册成功'})
        res.send({
            status: 0,
            code:20000,
            message:'注册成功',
        })

    })
    // res.send('reguser ok')
}


//登录函数
exports.login = (req,res) => {
    //接收表单数据
    const userinfo = req.body
	const sqlStr = 'select * from weixin where u_appid=?'
	db.query(sqlStr,userinfo.appid, (err,results) => {
		if(err){
		    // return res.send({ status:1 ,message: err.message })
		    return res.cc(err)
		}
		
		if(results.length > 0){
		    // return res.send({ status:1 ,message: '用户名已占用'})
		    return res.cc('账号已占用')
		}
		
		const sql = 'insert into weixin set ?'
		//调用插入语句
		db.query(sql,{ u_appid: userinfo.appid ,name: userinfo.nickName }, (err,results) =>{
			
			// if(err) return res.send( {status:1,message:err.message})
			if(err)return res.cc(err) 

			//判断影响行数
			if(results.affectedRows !== 1)return res.cc('失败')
			

			//注册用户成功
			// res.send({status:0,message:'注册成功'})
			res.send({
				status: 0,
				code:200,
				message:'注册成功',
			})

		})		
			
	})
	
		//定义插入新用户的 sql 语句
    


}



//获取更新月余额
exports.select_updata = ( req , res ) => {
	
    const userinfo = req.body
	const sql = "UPDATE `weixin` SET  money="+userinfo.money+" WHERE u_appid='"+userinfo.appid+"'";
    // const sql = "UPDATE `weixin` SET ? WHERE ?"
    db.query(sql,{ money: userinfo.money ,u_appid: userinfo.appid },(err,resulte) => {
        
        if(err) return res.cc('查询失败')
        
        res.send({
            code:20000,
            status: 0,
            message:'查询成功',
            data: resulte
        })
    })
}

//获取月余额
exports.select_getdate = (req ,res) => {
	    const userinfo = req.body
		const sql = "SELECT money FROM weixin WHERE ?";
		db.query(sql,{u_appid: userinfo.appid},(err,resulte) => {
		    
		    if(err) return res.cc('查询失败')
		    
		    res.send({
		        code:20000,
		        status: 0,
		        message:'查询成功',
		        data: resulte
		    })
		})
}

//插入事件
exports.select_goods = (req,res) => {
	const userinfo = req.body
	const sql = 'insert into kong_data set ?'
	// console.log(userinfo)
	db.query(sql,{  d_appid:userinfo.appid , da_time:userinfo.rqi , da_lei:userinfo.fenglei , da_money:userinfo.qian , da_beizhu:userinfo.beizhu },(err,resulte) => {
	    
	    if(err) return res.cc('查询失败')
	    
	    res.send({
	        status: 0,
	        message:'查询成功',
	        code:200
	    })
	})
}

//查询当月数据消费
exports.select_monthSpend = (req,res) => {
	const userinfo = req.body
    const sql = "SELECT * FROM kong_data WHERE DATE_FORMAT(da_time,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') and ?;"
    db.query(sql,{d_appid: userinfo.appid},(err,resulte) => {
        
        if(err) return res.cc('查询失败')
        
        res.send({
            status: 0,
            message:'查询成功',
			data: resulte,
        })
    })
}

//查询当天数据消费
exports.select_todaySpend = (req,res) => {
	
	const userinfo = req.body
	const sql = "SELECT * FROM kong_data WHERE TO_DAYS(da_time)=TO_DAYS(NOW()) and ?;"
    
    db.query(sql,{d_appid: userinfo.appid},(err,resulte) => {
        
        if(err) return res.cc('查询失败')
        
        res.send({
            status: 0,
            message:'查询成功',
			data: resulte
        })
    })
}

//查看明细
exports.select_mingxi = (req,res) => {
	
    const userinfo = req.body
	
    const sql = "SELECT * FROM kong_data  WHERE d_appid="+userinfo.appid+" and da_time='"+userinfo.date+"'";
    db.query(sql,(err,resulte) => {        
        if(err) return res.cc('查询失败')
		 res.send({
		    status: 0,
		    message:'查询成功',
		 	data: resulte
		 })
		 
    })
	
	// const sql1 = "SELECT SUM(da_money) FROM kong_data   WHERE ? and ?";
	// db.query(sql,{d_appid: userinfo.appid,da_time:userinfo.date},(err,resulte) => {
	    
	//     if(err) return res.cc('查询失败')
	    
	// 	this.v_data.num = resulte;
	// })
}


//查看报表
exports.select_baobiao = (req,res) => {
	
    const userinfo = req.body
	
	const sql =" SELECT * FROM kong_data   WHERE DATE_FORMAT(da_time,'%Y-%m')='"+userinfo.date+"'";
	
    db.query(sql,(err,resulte) => {        
        if(err) return res.cc('查询失败')
		 res.send({
		    status: 0,
		    message:'查询成功',
		 	data: resulte
		 })
		 
    })
	
}

exports.ce = (req,res) => {
    console.log(req.body)
    res.send({
        status: 0,
        message:'成功',
        code:200
    })
}

exports.ce = (req,res) => {
    console.log(req.body)
    res.send({
        status: 0,
        message:'成功',
        code:200
    })
}

exports.cedata = (req,res) => {
	

	// var obj = JSON.parse(List);
	let List ;
		//饼图
	let	videoData=[
			{
				name:'餐饮',
				value:2999
			},
			{
				name:'交通',
				value:1999
			},
			{
				name:'通讯',
				value:4999
			},
			{
				name:'购物',
				value:5999
			}
			// {
			// 	name:'魅族',
			// 	value:2979
			// },
			// {
			// 	name:'三星',
			// 	value:2997
			// }
		];
		// 柱状图
	// let	userData=[
	// 		{
	// 			date: '周一',
	// 			new: 5,
	// 			active:200
	// 		},
	// 		{
	// 			date: '周二',
	// 			new: 19,
	// 			active:550
	// 		},
	// 		{
	// 			date: '周三',
	// 			new: 62,
	// 			active:550
	// 		},
	// 		{
	// 			date: '周四',
	// 			new: 60,
	// 			active:800
	// 		},
	// 		{
	// 			date: '周五',
	// 			new: 65,
	// 			active:550
	// 		},
	// 		{
	// 			date: '周六',
	// 			new: 53,
	// 			active:770
	// 		},
	// 		{
	// 			date: '周日',
	// 			new: 33,
	// 			active:170
	// 		},
	// 	];
		//折线图
	let	orderData={
			date:['20191001','20191002','20191003','20191004','20191005','20191006','20191007'],
			data:List
		};
	let	tableData=[
			{
				name:'oppo',
				todayBuy:500,
				monthBuy:3500,
				totalBuy:22000
			},
			{
				name:'vivo',
				todayBuy:300,
				monthBuy:2200,
				totalBuy:24000
			},
			{
				name:'苹果',
				todayBuy:800,
				monthBuy:4500,
				totalBuy:65000
			},
			{
				name:'小米',
				todayBuy:1200,
				monthBuy:6500,
				totalBuy:45000
			},
			{
				name:'三星',
				todayBuy:300,
				monthBuy:2000,
				totalBuy:34000
			},
			{
				name:'魅族',
				todayBuy:350,
				monthBuy:3000,
				totalBuy:22000
			},
		]
    
	let userData=[
		{
			name:'今日',
			num:50
		},
		{
			name:'昨日',
			num:10
		}
	]
	List = [videoData,userData,orderData,tableData]
	res.send({
	    status: 0,
	    message:'成功',
		data:List,
	    code:200
	})
}